/**
 * Created by anzhicheng on 2017/7/18.
 * 只是一个综合性的js文件
 * 在主页和个人主页中加载显示不同的贴子已经用户时使用这里的函数
 */
//主页部分
function showAtRight(btn){//展示帖子

    //改变左侧选中图片
    $("#recommend").css("background-image","url(img/推荐.jpg)");
    $("#study").css("background-image","url(img/学习.jpg)");
    $("#equity").css("background-image","url(img/权益.jpg)");
    $("#activity").css("background-image","url(img/活动.jpg)");
    $("#life").css("background-image","url(img/生活.jpg)");
    $("#schoolmate").css("background-image","url(img/校友.jpg)");

    var imgtype = btn.value;
    if (imgtype === '推荐'){
        $("#recommend").css("background-image","url(img/推荐-点击.jpg)");
    }else if (imgtype === '学习'){
        $("#study").css("background-image","url(img/学习-点击.jpg)");
    }else if (imgtype === '权益'){
        $("#equity").css("background-image","url(img/权益-点击.jpg)");
    }else if (imgtype === '活动'){
        $("#activity").css("background-image","url(img/活动-点击.jpg)");
    }else if (imgtype === '生活'){
        $("#life").css("background-image","url(img/生活-点击.jpg)");
    }else if (imgtype === '校友'){
        $("#schoolmate").css("background-image","url(img/校友-点击.jpg)");
    }




    //获取查询参数
    // var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    // var searchContent = parms[0].split('=');
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("passageArea").innerHTML = xmlhttp.responseText;
        }
    }
    var invalue = btn.value;
    if (isNaN(invalue)){
        xmlhttp.open("GET","postContent.jsp?category="+btn.value+"&need=allpost",true);
    }else {
        xmlhttp.open("GET","postContent.jsp?targetPage="+btn.value+"&need=allpost",true);
    }
    xmlhttp.send();

}





//个人主页
function loadMyActPages(btn){//加载帖子
    //获取用户名
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+btn.value+"&need=active",true);
    xmlhttp.send();

    $("#postBtn").css("border-bottom","none");
    $("#collectionBtn").css("border-bottom","none");
    $("#actionBtn").css("border-bottom"," 3px solid #00e6d0");
}


function loadFollow(obj) {//加载关注
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+obj.value+"&need=follow",true);
    xmlhttp.send();

    $("#fanBtn").css("background-color","#ffffff").css("color","#00e6d0");
    $("#followBtn").css("background-color","#00e6d0").css("color","#ffffff");
}
function loadFans(obj) {//加载粉丝
    //获取用户名
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+obj.value+"&need=fan",true);
    xmlhttp.send();

    $("#followBtn").css("background-color","#ffffff").css("color","#00e6d0");
    $("#fanBtn").css("background-color","#00e6d0").css("color","#ffffff");
}
function loadMyCol(o) {

    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+o.value+"&need=collection",true);
    xmlhttp.send();

    $("#postBtn").css("border-bottom","none");
    $("#actionBtn").css("border-bottom","none");
    $("#collectionBtn").css("border-bottom"," 3px solid #00e6d0");
}


//搜索跳转
function searchByKey() {

    window.location.href = "searchResult.jsp?search="+$("#search").val();

}

//搜索内容加载
function loadResultUsers(obj) {
    //获取查询参数
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("usersResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "headShow.jsp?targetPage=" + obj.value + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}

function loadResultPosts(obj) {
    //获取查询参数
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("postsResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "postContent.jsp?targetPage=" + obj.value + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}



function mylike(obj) {//点赞
    var postID = (obj.id.split('+'))[1];
    var todo = obj.innerText;
    $.ajax({
            url: '/AddCollection',
            type: "POST",
            data: {
                postID: postID,
                action:"like",
                todo:todo
            }
            ,
            success: function (data) {
                if (todo==='赞'){
                    obj.innerText = '已赞';
                    var likes = obj.parentNode.children[1].innerText;
                    var newlikes = parseInt(likes)+1;
                    obj.parentNode.children[1].innerText = newlikes+"人已赞";

                }else if(todo === '已赞'){
                    obj.innerText = '赞';
                    var likes = obj.parentNode.children[1].innerText;
                    var newlikes = parseInt(likes)-1;
                    obj.parentNode.children[1].innerText = newlikes+"人已赞";
                }
            }
            ,
            error: function (err) {
                console.log(err);
            }
        }
    );
}

function mycollect(obj) {//收藏
    var postID = (obj.id.split('+'))[1];
    var todo = obj.innerText;
    $.ajax({
            url: '/AddCollection',
            type: "POST",
            data: {
                postID: postID,
                action:"collection",
                todo:todo
            }
            ,
            success: function (data) {
                if (todo==='收藏'){
                    obj.innerText = '已收藏';
                }else if(todo === '已收藏'){
                    obj.innerText = '收藏';
                }
            }
            ,
            error: function (err) {
                console.log(err);
            }
        }
    );
}

function mydelete(obj) {//删除
    var postID = (obj.id.split('+'))[1];
    $.ajax({
            url: '/DeletePostServlet',
            type: "POST",
            data: {
                postID: postID
            }
            ,
            success: function (data) {
                //alert("删除成功");
                window.location.reload();
            }
            ,
            error: function (err) {
                console.log(err);
            }
        }
    );
}

function loadPersonalHomePage(o){
    window.location.href = "giggleOtherHomePage.jsp?username="+o.innerText;
}


//他人主页部分
function changeConcernValue(btn) {
    btn.value="取消关注";
}

function changeBackValue(btn) {
    btn.value="已关注";
}

//发帖
function publishPost() {
    window.location.href = "PostPublish.jsp";
}


//头像分页
function loadLastFollow(btn) {
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==1){
        targetPage=maxPage;
    }else{
        targetPage = currentPage-1;
    }
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+targetPage+"&need=follow",true);
    xmlhttp.send();

}

function loadNextFollow(btn) {
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==maxPage){
        targetPage=1;
    }else{
        targetPage = currentPage+1;
    }
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+targetPage+"&need=follow",true);
    xmlhttp.send();

}


function loadLastFans(btn) {//加载粉丝
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==1){
        targetPage=maxPage;
    }else{
        targetPage = currentPage-1;
    }
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+targetPage+"&need=fan",true);
    xmlhttp.send();
}

function loadNextFans(btn) {//加载粉丝
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==maxPage){
        targetPage=1;
    }else{
        targetPage = currentPage+1;
    }
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("headDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","headShow.jsp?username="+tousername+"&targetPage="+targetPage+"&need=fan",true);
    xmlhttp.send();
}

//搜索内容加载
function loadLastResultUsers(btn) {
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==1){
        targetPage=maxPage;
    }else{
        targetPage = currentPage-1;
    }
    //获取查询参数
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("usersResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "headShow.jsp?targetPage=" + targetPage + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}

//搜索内容加载
function loadNextResultUsers(btn) {
    var params = btn.id.split('|');//当前按钮值|按钮最大值
    var currentPage = parseInt(params[0]);
    var maxPage = parseInt(params[1]);

    var targetPage;
    if(currentPage==maxPage){
        targetPage=1;
    }else{
        targetPage = currentPage+1;
    }
    //获取查询参数
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("usersResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "headShow.jsp?targetPage=" + targetPage + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}


function showLastAtRight(btn){//展示帖子
    //获取查询参数
    var targetpage = parseInt(btn.id)-1;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("passageArea").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?targetPage="+targetpage+"&need=allpost",true);
    xmlhttp.send();
}


function showNextAtRight(btn){//展示帖子
    //获取查询参数
    var targetpage = parseInt(btn.id)+1;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("passageArea").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?targetPage="+targetpage+"&need=allpost",true);
    xmlhttp.send();
}


function loadLastMyActPages(btn){//加载帖子
    //获取用户名
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var targetPage = parseInt(btn.id)-1;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=active",true);
    xmlhttp.send();
}

function loadNextMyActPages(btn){//加载帖子
    //获取用户名
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var targetPage = parseInt(btn.id)+1;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=active",true);
    xmlhttp.send();
}


function loadLastMyCol(btn) {
    var targetPage = parseInt(btn.id)-1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=collection",true);
    xmlhttp.send();
}

function loadNextMyCol(btn) {
    var targetPage = parseInt(btn.id)+1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=collection",true);
    xmlhttp.send();
}




function loadLastResultPosts(btn) {
    //获取查询参数
    var targetPage = parseInt(btn.id)-1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("postsResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "postContent.jsp?targetPage=" + targetPage + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}


function loadNextResultPosts(btn) {
    //获取查询参数
    var targetPage = parseInt(btn.id)+1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var searchContent = parms[0].split('=');
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("postsResult").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "postContent.jsp?targetPage=" + targetPage + "&need=search&search=" +searchContent[1], true);
    xmlhttp.send();
}

function jumptoSelfHomePage(o) {

    var id = (o.id.split('+'))[1];
    $.ajax({
            url: '/AddCollection',
            type: "POST",
            data: {
                postID: id,
                action:"addHit"
            }
            ,
            success: function (data) {

            }
            ,
            error: function (err) {
                console.log(err);
            }
        }
    );
}


function zhuxiao() {
	$.ajax({
        url: 'user/unsubscribe',
        type: "GET",
        data: {
        }
        ,
        success: function (data) {
        }
        ,
        error: function (err) {
            console.log(err);
        }
    }
);
}

//我的帖子
function loadMyPos(o){
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+o.value+"&need=mypost",true);
    xmlhttp.send();
    $("#collectionBtn").css("border-bottom","none");
    $("#actionBtn").css("border-bottom","none");
    $("#postBtn").css("border-bottom"," 3px solid #00e6d0");
}

function loadLastMyPos(btn){
    var targetPage = parseInt(btn.id)-1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=mypost",true);
    xmlhttp.send();
}

function loadNextMyPos(btn){
    var targetPage = parseInt(btn.id)+1;
    var parms = decodeURIComponent(window.location.search.substr(1)).split('&');
    var tousername = (parms[0].split('='))[1];
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("myContent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","postContent.jsp?username="+tousername+"&targetPage="+targetPage+"&need=mypost",true);
    xmlhttp.send();
}